<?php

/*
 * @copyright :     Ienter asia Technologies
 * @author :        Hunglv
 * @version         v.1.0
 * @created         20/6/2014
 * @Description :   Model Employee Bonus
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Mmod_employeebonus extends MYAD_Model {

    protected $table_name = 'm_employeebonus';
    protected $table_employee = 'm_employee';

    function __construct() {
        parent::__construct();
    }

    /*
     * @author :        Hunglv
     * @created:        25/6/2014
     * @Description:    Get all data for employee bonus
     * @Input :         (Integer) $page
     * @Output :        (Array) data for view data
     */

    function getAllData($employeeID = 0, $page = 0, $employee = null) {
        if (!$employee) {
            redirect('/employee');
        }
        /* load all label */
        $labelData = $this->getLabelData();

        /* load all employee data range */
        $allData = $this->lblBonusInfo($employeeID);

        /* load pagination */
        $pagiData = array(
            'base_url' => base_url('employee/bonus/'),
            'total_rows' => count($allData['data']),
            'uri_segment' => 3,
        );
        $pagination = $this->config->item('pagination');
        $offset = isset($pagiData['per_page']) ? $pagiData['per_page'] : $pagination['per_page'];
        $pagi = $this->getPagination($pagiData);

        /* load salaryinfo data range */
        $salaryData = $this->lblBonusInfo($employeeID, $page, $offset);

        /* get employee name */
        $employeeName = '';
        if ($employee) {
            $employeeName = $employee->EmployeeName;
        }

        $data = array(
            'show' => 'employeebonuslist',
            'pagi' => $pagi,
            'nullcharacter' => $this->lang->line('nullcharacter'),
            'message' => $this->lang->line('emp_bu_table_empty_data'),
            'order' => $page,
            'employeeID' => $employeeID,
            'employeeName' => $employeeName,
        );
        return array_merge($data, $labelData, $salaryData);
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    Get label data of leave info
     * @Input :         void
     * @Output :        (Array) data label
     */

    function getLabelData() {

        $title = $this->lang->line('emp_bu_title');
        $lblemployeeName = $this->lang->line('emp_bu_employee_name');
        $lblnewRegister = $this->lang->line('emp_bu_new_register');
        $lblOrder = $this->lang->line('emp_bu_table_col_order');
        $lblApplyDate = $this->lang->line('emp_bu_table_col_ApplyDate');
        $lblBonus = $this->lang->line('emp_bu_table_col_Bonus');
        $lblBonusInfo = $this->lang->line('emp_bu_table_col_BonusInfo');
        $lblAction = $this->lang->line('emp_bu_table_col_Action');

        $data = array(
            'title' => (empty($title) ? 'List Employee Bonus Information' : $title),
            'lblemployeeName' => (empty($lblemployeeName) ? 'Employee name' : $lblemployeeName),
            'lblnewRegister' => (empty($lblnewRegister) ? 'New register' : $lblnewRegister),
            'lblOrder' => (empty($lblOrder) ? 'Order' : $lblOrder),
            'lblApplyDate' => (empty($lblApplyDate) ? 'Apply Date' : $lblApplyDate),
            'lblBonus' => (empty($lblBonus) ? 'Bonus' : $lblBonus),
            'lblBonusInfo' => (empty($lblBonusInfo) ? 'Bonus Info' : $lblBonusInfo),
            'lblAction' => (empty($lblAction) ? 'Action' : $lblAction),
        );
        return $data;
    }

    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Get Bonus info by employeeID
     * @Input :         
     *                  (Integer) $employeeID
     *                  (Integer) $limit
     *                  (Integer) $offset
     * @Output :        Array data /FALSE
     */

    function lblBonusInfo($employeeID = 0, $limit = Null, $offset = Null) {
        /* get bonusinfo */
        $this->db->from($this->table_name);
        $this->db->where('EmployeeId', $employeeID);
        if ($limit !== Null && $offset !== Null) {
            $this->db->limit($offset, $limit);
        } elseif ($limit !== NULL) {
            $this->db->limit($limit);
        }
        $query = $this->db->get();
        $result = $query->result();
        $temp = FALSE;
        if (count($result)) {
            $temp = $result;
        }

        return array('data' => $temp);
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    check validate form add/edit employee bonus
     * @Input :         void
     * @Output :        boolean
     */

    function validate() {
        $_POST['txtApplyDate'] = mb_convert_kana($_POST['txtApplyDate'], 'a');
        $_POST['txtBonus'] = mb_convert_kana($_POST['txtBonus'], 'a');       
        $_POST['txtBonusInfo'] = mb_convert_kana($_POST['txtBonusInfo'], 'a');       
        $this->form_validation->set_error_delimiters('', '');
        $this->form_validation->set_rules('txtApplyDate', $this->lang->line('emp_bu_table_col_ApplyDate'), 'required|max_length[7]|callback_validate_month');
        $this->form_validation->set_rules('txtBonus', $this->lang->line('emp_bu_table_col_Bonus'), 'required|greater_than[0]|numeric');
        $this->form_validation->set_rules('txtBonusInfo', $this->lang->line('emp_bu_table_col_BonusInfo'), 'max_length[255]');
        return $this->form_validation->run();
    }

    /*
     * @author :        Hunglv
     * @created:        20/6/2014
     * @Description:    update Bonus info
     * @Input :         (Integer) $id
     * @Output :        boolean
     */

    function updateEmployeeBonusInfo($id = 0, $ApplyDate = Null) {
        $applyDates = strtotime($this->input->post('txtApplyDate') . '/1');
        //echo $applyDates.'-'.$ApplyDate; die();
        if ($applyDates != $ApplyDate) {
            $conditionDuplicate = array(
                'EmployeeId' => $id,
                'ApplyDate' => $this->input->post('txtApplyDate').'/01',
            );
            $duplicate = $this->checkDuplicate($this->table_name, $conditionDuplicate);
            if ($duplicate) {
                return array(
                    'flag' => FALSE,
                     'message' => $this->lang->line('MC012')
                );
            }
        }
        $data = array(
            'EmployeeId' => (integer)$id,
            'ApplyDate' => date('Y-m-01', $applyDates),
            'Bonus' => (integer)$this->input->post('txtBonus'),
            'BonusInfo' => dQuotes($this->input->post('txtBonusInfo')),
            'Del_Flag' => ($this->input->post('chkDelete')=='on') ? 1 : 0,
        );
        $condition = array();
        if (!empty($ApplyDate)) {
            $condition['ApplyDate'] = date('Y-m-01', $ApplyDate);
            $condition['EmployeeId'] = (integer)$id;
        }
        
        $temp = $this->updateDB($this->table_name, $data, $condition);
        if ($temp) {
            return array(
                'flag' => TRUE,
                'message' => $this->lang->line('emp_updateDB_success')
            );
        } else {
            return array(
                'flag' => FALSE,
                'message' => $this->lang->line('emp_updateDB_fall')
            );
        }
    }

    /*
     * @author :        Hunglv
     * @created:        23/6/2014
     * @Description:    basic data for new add leave info
     * @Input :         (Interger) $id
     * @Input :         (Interger) $Year
     * @Output :        Array
     */

    function getAddBonusInfo($id = 0, $ApplyDate = Null, $employee = false) {

        if (!$employee) {
            redirect('/employee');
        }
        /* get language info */
        $title=(is_null($ApplyDate)) ?  $this->lang->line('emp_bu_add_title') : $this->lang->line('emp_bu_update_title') ;
        $lblemployeeName = $this->lang->line('emp_bu_employee_name');
        $lblnewRegister = $this->lang->line('emp_bu_new_register');
        $lblOrder = $this->lang->line('emp_bu_table_col_order');
        $lblApplyDate = $this->lang->line('emp_bu_table_col_ApplyDate');
        $lblBonus = $this->lang->line('emp_bu_table_col_Bonus');
        $lblBonusInfo = $this->lang->line('emp_bu_table_col_BonusInfo');
        $btnSubmit = $this->lang->line('emp_bu_submit');
        $btnBack = $this->lang->line('emp_bu_back');
        $lblAction = $this->lang->line('emp_bu_table_col_Action');
        $dialogTitle = $this->lang->line('emp_dialog_title');
        /* get employee info */
        $bonusInfo = $this->getBonusInforByIDAndApplyDate($id, $ApplyDate);
        if ($id && !isset($_POST['btnSubmit']) && $bonusInfo) { 
            /* is load default */
            $emp = array(
                'txtApplyDate' => $this->setDate($bonusInfo->ApplyDate, 'Y/m'),
                'txtBonus' => $bonusInfo->Bonus,
                'txtBonusInfo' => $bonusInfo->BonusInfo,
                'txtDateYear' => $this->setDate($bonusInfo->ApplyDate, 'Y'),
                'txtDateMonth' => $this->setDate($bonusInfo->ApplyDate, 'm')-1,
                'chkDelete' =>$bonusInfo->Del_Flag,
                'EmployeebonusId'=>$bonusInfo->EmployeebonusId,
            );
        } else { 
            $emp = array(
                'txtApplyDate'  => set_value('txtApplyDate'),
                'txtBonus'      => set_value('txtBonus'),
                'txtBonusInfo'  => set_value('txtBonusInfo'),
                'txtDateYear'   => date('Y'),
                'txtDateMonth'  => date('m')-1,     
                'chkDelete' => ($this->input->post('chkDelete')=='on') ? 1 : 0,
                'EmployeebonusId'  => $this->input->post('txtEmployeebonusId'),                
            );
        }

        $temp = array(
            'js' => array('jquery-ui.js'),
            'css' => array('jquery-ui.css'),            
            'show' => 'add_employeebonus',
            /* set label */
            'title' => (empty($title) ? 'List Employee Bonus Information' : $title),
            'lblemployeeName' => (empty($lblemployeeName) ? 'Employee name' : $lblemployeeName),
            'employeeName' => (empty($employee) ? '' : $employee->EmployeeName),
            'lblnewRegister' => (empty($lblnewRegister) ? 'New register' : $lblnewRegister),
            'lblOrder' => (empty($lblOrder) ? 'Order' : $lblOrder),
            'lblApplyDate' => (empty($lblApplyDate) ? 'Apply Date' : $lblApplyDate),
            'lblBonus' => (empty($lblBonus) ? 'Bonus' : $lblBonus),
            'dialogTitle' => (empty($dialogTitle) ? 'Notification' : $dialogTitle),
            'lblBonusInfo' => (empty($lblBonusInfo) ? 'Bonus Info' : $lblBonusInfo),
            'btnBack' => (empty($btnBack) ? 'Back' : $btnBack),
            'btnSubmit' => (empty($btnSubmit) ? 'Submit' : $btnSubmit),            
            'lblAction' => (empty($lblAction) ? 'Action' : $lblAction),
            'minYear' => $this->session->userdata('minYear'),
            'maxYear' => $this->session->userdata('maxYear'),            
            /* set data */
            'id' => $id,
        );
        return array_merge($temp, $emp);
    }
    /*
     * @author :        Hunglv
     * @created:        
     * @Description:    Check exist
     * @Input :         (Interger) $ApplyDate
     * @Input :         (Interger) $employeeID
     * @Output :        return true or false
     * 
     */

    function checkExist($employeeID,$ApplyDate) {
        /* get salaryinfo */
        $this->db->from($this->table_name);
        $this->db->where('ApplyDate', date('Y-m-d',$ApplyDate));
        $this->db->where('EmployeeId', $employeeID);
        $query = $this->db->get();
        if ($query->num_rows()>0) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
    /*
     * @author :        Hunglv
     * @created:        25/6/2014
     * @Description:    basic data for new add leave info
     * @Input :         (Interger) $id
     * @Input :         (Interger) $year
     * @Output :        Array
     * 
     */

    function getBonusInforByIDAndApplyDate($id = 0, $date = Null) {
        /* get salaryinfo */
        $this->db->from($this->table_name);
        $this->db->where('EmployeeId', $id);
        $this->db->where('ApplyDate', date('Y-m-d', $date));
        $query = $this->db->get();
        $result = $query->result();
        if (count($result)) {
            return $result[0];
        } else {
            return FALSE;
        }
    }
    
    /*
     * @author :        Quyet
     * @created:        20/6/2014
     * @Description:    get employee
     * @Input :         (Integer) $id
     * @Output :        object/false
     */

    function getEmployee($id = 0) { 
        $query = $this->db->get_where($this->table_employee, array('EmployeeId' => $id,'CompanyCode'=>$this->session->userdata('CompanyCode')));
        $result = $query->result();        
        if (count($result)) {
            return $result[0];
        } else {
            return FALSE;
        }
    }

}

?>
